package com.example.chengww.myone.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.example.chengww.myone.R;
import com.example.chengww.myone.bean.Constants;
import com.example.chengww.myone.bean.Data;
import com.example.chengww.myone.utils.HTTPUtils;
import com.google.gson.Gson;

import java.io.InputStream;
import java.util.ArrayList;

/**
 * 启动页，第一次启动进入引导页，否则进入主页
 */
public class SplashActivity extends AppCompatActivity {

    private Intent intent;
    private ArrayList<Data> datas;
    private final static int GUIDE_ACTIVITY = 100;
    private final static int MAIN_ACTIVITY = 101;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case GUIDE_ACTIVITY:
                    intent = new Intent(SplashActivity.this,GuideActivity.class);
                    break;
                case MAIN_ACTIVITY:
                    intent = new Intent(SplashActivity.this,MainActivity.class);
                    break;
            }
            intent.putParcelableArrayListExtra("datas",datas);
            startActivity(intent);
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        SharedPreferences mSharedPreferences = getSharedPreferences("isFirstEntry",
                Activity.MODE_PRIVATE);
        final boolean isGuide = mSharedPreferences.getBoolean("isGuide",true);


        new Thread(new Runnable() {
            @Override
            public void run() {
                if (datas == null || datas.size() == 0){
                    datas = new ArrayList<>();
                    //所有详情地址
                    InputStream in = HTTPUtils.getInputStream(Constants.HOME.HOME_PATH);
                    String string = HTTPUtils.getStringByInputStream(in);
                    int start = string.indexOf("[") + 2;
                    int end = string.indexOf("]") - 1;
                    String subStr = string.substring(start,end);
                    String[] strArr = subStr.split("\",\"");
                    for (int i = 0; i < strArr.length; i++) {
                        Log.e("===strarr",strArr[i]);
                        String path = Constants.HOME.HOME_DETAIL_PATH + strArr[i];
                        String json = HTTPUtils.getStringByInputStream(
                                HTTPUtils.getInputStream(path));
                        Log.e("===json",json);
                        String myJson = json.substring(json.indexOf(":{")+1,json.length()-1);
                        Log.e("===myJson",myJson);
                        Gson gson = new Gson();
                        Data data = gson.fromJson(myJson,Data.class);
                        Log.e("===data",data.toString());
                        datas.add(data);
                    }
                    if (isGuide){
                        handler.obtainMessage(GUIDE_ACTIVITY).sendToTarget();
                    }else {
                        handler.obtainMessage(MAIN_ACTIVITY).sendToTarget();
                    }
                }
            }
        }).start();

    }
}
